package com.itwk.yygh.cmn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwk.yygh.cmn.mapper.DictMapper;
import com.itwk.yygh.cmn.service.DictService;
import com.itwk.yygh.hosp.model.cmn.Dict;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.logging.XMLFormatter;
import java.util.stream.Collectors;

/**
 * @author wangke
 * @version 1.0.0
 * @Description TODO
 * @createTime 2021年08月21日 16:33:00
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Override
    public List<Dict> findChildData(Long id) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("parent_id",id);
        List<Dict> dicts = baseMapper.selectList(dictQueryWrapper);
        //判断是否还有子节点
        List<Dict> list = dicts.stream().map(dict -> {
            boolean isChildData = isChildData(dict.getId());
            dict.setHasChildren(isChildData);
            return dict;
        }).collect(Collectors.toList());

        return list;
    }

    //判断是否还有子节点方法
    public boolean isChildData(Long id){
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("parent_id",id);
        Integer count = baseMapper.selectCount(dictQueryWrapper);
        return count>0;

    }


}
